///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_OS_CMIMETYPE_H_
#define _BUOLA_OS_CMIMETYPE_H_

#include <buola/buola.h>
#include <vector>
#include <unordered_map>
#include <map>

namespace buola { namespace os {

///////////////////////////////////////////////////////////////////////
////////////////////// Class CMIMEType ////////////////////////////////
///////////////////////////////////////////////////////////////////////
//
// This class represents a file type. It also contains static members
// with lists of all file types and magics
//
///////////////////////////////////////////////////////////////////////

class CMIMEType
{
private:
    struct SGlob;
    struct SMagic;
    
    enum EInit
    {
        INIT_NONE=0,
        INIT_ALIASES=(1<<0),
        INIT_APPCACHE=(1<<1),
        INIT_GLOBS=(1<<2),
        INIT_MAGICS=(1<<3),
        INIT_ALLLOADED=(1<<4)
    };
    DECLARE_FRIEND_ENUM_BITOPS(EInit)

private:
    CMIMEType(const std::string &pType);

public:
//interface
    const std::string &GetType()        {   return mType;       }
    const std::string &GetMedia()       {   return mMedia;      }
    const std::string &GetSubType()     {   return mSubType;    }
    const std::wstring &GetComment()    {   return mComment;    }

    std::string GetIcon(os::CIconTheme *pTheme);

    int FillActionList(std::vector<CFileAction*>&);

public:
// getting the mime type from a file or from a mime name
    static CMIMEType *Get(const std::string&);
    static void GetAll(std::vector<CMIMEType*>&);

    static std::string Guess(const io::CFile &pFile);
    static std::string Guess(const io::CURI &pURI,bool pGlobOnly);
        
    static void LoadAll();

private:
    static void InitAliases();
    static void InitAppCache();
    static void InitGlobs();
    static void InitMagics();
    
    static recursive_mutex mStaticMutex;
    static std::unordered_map<std::string,CMIMEType*> mTypes;

    static EInit mInit;
    static std::unordered_map<std::string,std::string> mAliases;
    static std::unordered_multimap<std::string,std::string> mAppCache;
    static std::vector<SGlob*> mGlobs;
    static std::vector<SMagic*> mMagics;

private:
    std::vector<std::string> mSuperClasses;

    std::string mType;
    std::string mMedia;
    std::string mSubType;
    std::wstring mComment;
    std::map<CIconTheme*,std::string> mIcons;
};

/*namespace os*/ } /*namespace buola*/ }

#endif
